package com.ptkc.core.esearch.repos;

import com.ptkc.core.esearch.model.DeliverCost;
import com.ptkc.core.esearch.model.DeliverCostTimeslot;
import com.ptkc.utils.ESUtil;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Mr.Jiao
 * @date: 2018年12月15日
 */

@Component
public class DeliverCostRepository {

    private ESUtil esUtil = null;

    private RestHighLevelClient restHighLevelClient;

    private final String INDEX = "runfast_deliver_cost";

    private final String TYPE = "delivercost";

    public DeliverCostRepository(RestHighLevelClient restHighLevelClient){
        this.restHighLevelClient = restHighLevelClient;
        this.esUtil = new ESUtil(restHighLevelClient);
    }

    public boolean save(DeliverCost cost) throws IOException {
        if(cost != null) {
            DocWriteResponse response = this.esUtil.save(INDEX, TYPE, String.valueOf(cost.getId()), cost);
            return (response.status() == RestStatus.OK || response.status() == RestStatus.CREATED);
        }
        return false;
    }

    public boolean del(Integer costId) throws IOException {
        if(costId != null){
            DeleteRequest request = new DeleteRequest(INDEX, TYPE, String.valueOf(costId));
            DeleteResponse response = this.restHighLevelClient.delete(request);
            if(response.status() == RestStatus.OK){
                return true;
            }
        }
        return false;
    }

    public boolean saveTimeCost(DeliverCostTimeslot timeslot) throws IOException {
        if(timeslot != null && timeslot.getCostid() != null && timeslot.getId() != null){
            UpdateRequest request = new UpdateRequest(INDEX, TYPE, String.valueOf(timeslot.getCostid()));
            Map<String, Object> params = new HashMap<>();
            params.put("timeslots", BeanMap.create(timeslot));
            Script script = new Script(ScriptType.STORED, null, "runfast_delivercost_nested_timeslot_save", params);
            request.script(script);
            UpdateResponse response = this.restHighLevelClient.update(request);
            if(response.status() == RestStatus.OK || response.status() == RestStatus.CREATED){
                return true;
            }
        }
        return false;
    }

    public boolean delTimeCost(DeliverCostTimeslot timeslot) throws IOException {
        if(timeslot != null && timeslot.getCostid() != null && timeslot.getId() != null){
            UpdateRequest request = new UpdateRequest(INDEX, TYPE, String.valueOf(timeslot.getCostid()));
            Map<String, Object> params = new HashMap<>();
            params.put("timeslots", BeanMap.create(timeslot));
            Script script = new Script(ScriptType.STORED, null, "runfast_delivercost_nested_timeslot_del", params);
            request.script(script);
            UpdateResponse response = this.restHighLevelClient.update(request);
            if(response.status() == RestStatus.OK || response.status() == RestStatus.CREATED){
                return true;
            }
        }
        return false;
    }

//    public DeliverCost getDefaultDeliverCost(Integer agentId) {
//        SearchRequest request = new SearchRequest();
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        BoolQueryBuilder parentQuery = QueryBuilders.boolQuery();
//        parentQuery.must(QueryBuilders.termQuery("isdefault", 1));
//        parentQuery.must(QueryBuilders.termQuery("agentId", agentId));
//        sourceBuilder.query(parentQuery).size(1).from(0);
//        request.source(sourceBuilder).indices(INDEX).types(TYPE);
//        SearchResponse response = null;
//        try {
//            response = this.restHighLevelClient.search(request);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//        if(response.status() == RestStatus.OK){
//            SearchHits searchHits = response.getHits();
//            if(searchHits != null){
//                SearchHit[] hits = searchHits.getHits();
//                if(hits != null && hits.length > 0) {
//                    SearchHit hit = hits[0];
//                    if(hit != null) {
//                        String dataStr = hit.getSourceAsString();
//                        DeliverCost cost = null;
//                        try {
//                            cost = JSON.parseObject(dataStr, DeliverCost.class);
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
//                        if(cost != null){
//                            if(cost.getTimeslots() == null){
//                                cost.setTimeslots(new ArrayList<>());
//                            }
//                            return cost;
//                        }
//                    }
//                }
//            }
//        }
//        return null;
//    }

}
